﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Reflection;
using System.Diagnostics;
using Microsoft.SharePoint.Administration;

namespace CKS.Sandbox.Admin.Layouts.CKS.Sandbox.Admin
{
    public partial class GeneralSettings : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Assembly asm = Assembly.GetExecutingAssembly();            
            FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(asm.Location);
            litVersion.Text = fvi.FileVersion;

            btnSave.Click += new EventHandler(btnSave_Click);
            btnCancel.Click += new EventHandler(btnCancel_Click);

            if (!Page.IsPostBack)
            {
                LoadDropDowns();

                chkEnforceRules.Checked = Utility.GeneralSettings.IsValidationEnforced();
                chkEnableEmail.Checked = Utility.GeneralSettings.IsEmailNotificationEnabled();
                txtEmailSendTo.Text = Utility.GeneralSettings.GetEmailSendToAddress();
            }

            txtEmailSendTo.Enabled = chkEnableEmail.Checked;
        }

        private void LoadDropDowns()
        {
            ddlFileTypes.SelectedValue = SPFarm.Local.Properties[Utility.FileTypes.FarmProperty].ToString();
            ddlTokenBehaviour.SelectedValue = SPFarm.Local.Properties[Utility.PublicKeyTokens.FarmProperty].ToString();
            ddlSolutionIdBehaviour.SelectedValue = SPFarm.Local.Properties[Utility.SolutionIds.FarmProperty].ToString();
            ddlAssemblyNameBehaviour.SelectedValue = SPFarm.Local.Properties[Utility.AssemblyNames.FarmProperty].ToString();
            ddlTypeNamespaceBehaviour.SelectedValue = SPFarm.Local.Properties[Utility.AssemblyNameSpaces.FarmProperty].ToString();

            // controls removed - no need to specify these - they are "Allow" by default
            //ddlFileTextString.SelectedValue = SPFarm.Local.Properties[Utility.FileRegEx.FarmProperty].ToString();
            //ddlFileRegEx.SelectedValue = SPFarm.Local.Properties[Utility.FileTextString.FarmProperty].ToString();
        }

        void btnSave_Click(object sender, EventArgs e)
        {
            // Save the Values to the Properties Hashtable
            SPFarm.Local.Properties[Utility.FileTypes.FarmProperty] = ddlFileTypes.SelectedValue;
            SPFarm.Local.Properties[Utility.PublicKeyTokens.FarmProperty] = ddlTokenBehaviour.SelectedValue;
            SPFarm.Local.Properties[Utility.SolutionIds.FarmProperty] = ddlSolutionIdBehaviour.SelectedValue;
            SPFarm.Local.Properties[Utility.AssemblyNameSpaces.FarmProperty] = ddlTypeNamespaceBehaviour.SelectedValue;
            SPFarm.Local.Properties[Utility.AssemblyNames.FarmProperty] = ddlAssemblyNameBehaviour.SelectedValue;

            SPFarm.Local.Properties[Utility.GeneralSettings.ValidationEnabledProperty] = chkEnforceRules.Checked;
            SPFarm.Local.Properties[Utility.GeneralSettings.EmailEnabledProperty] = chkEnableEmail.Checked;
            SPFarm.Local.Properties[Utility.GeneralSettings.EmailSendToFarmProperty] = txtEmailSendTo.Text;

            // controls removed - no need to specify these - they are "Allow" by default
            //SPFarm.Local.Properties[Utility.FileRegEx.FarmProperty] = ddlFileRegEx.SelectedValue;
            //SPFarm.Local.Properties[Utility.FileTextString.FarmProperty] = ddlFileTextString.SelectedValue;
            
            // save the changes
            SPFarm.Local.Update();
            Response.Redirect("/");
        }

        void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("/");
        }
    }
}
